package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class DeadLetterMqConfig {

    //声明参数
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public DirectExchange exchange(){
        //直接返回
        return new DirectExchange(exchange_dead,true,false);
    }

    /**
     * 创建队列1
     * @return
     */
    @Bean
    public Queue queue1(){
        //设置额外的参数
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("x-dead-letter-exchange",exchange_dead);
        hashMap.put("x-dead-letter-routing-key",routing_dead_2);
        //10秒
        hashMap.put("x-message-ttl",10000);
        //  返回队列对象
        return new Queue(queue_dead_1,true,false,false,hashMap);
    }

    /**
     * 创建绑定关系！
     * @return
     */
    @Bean
    public Binding binding1() {
        //  交换机 --- key1 --- queue1
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }

    /**
     * 创建队列2
     * @return
     */
    @Bean
    public Queue queue2(){
        return new Queue(queue_dead_2,true,false,false);
    }

    /**
     * 设置一个绑定关系
     * @return
     */
    @Bean
    public Binding binding2(){
        //  交换机 --- key2 --- queue2
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }

}
